home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / gi / docstring.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  5KB  |  154 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. from _gi import VFuncInfo, FunctionInfo, CallableInfo, ObjectInfo, StructInfo, Direction, TypeTag
  5. _generate_doc_string_func = None
  6.  
  7. def set_doc_string_generator(func):
  8.     '''Set doc string generator function
  9.  
  10.     :param callable func:
  11.         Callable which takes a GIInfoStruct and returns documentation for it.
  12.     '''
  13.     global _generate_doc_string_func
  14.     _generate_doc_string_func = func
  15.  
  16.  
  17. def get_doc_string_generator():
  18.     '''Returns the currently registered doc string generator.'''
  19.     return _generate_doc_string_func
  20.  
  21.  
  22. def generate_doc_string(info):
  23.     '''Generate a doc string given a GIInfoStruct.
  24.  
  25.     :param gi.types.BaseInfo info:
  26.         GI info instance to generate documentation for.
  27.     :returns:
  28.         Generated documentation as a string.
  29.     :rtype: str
  30.  
  31.     This passes the info struct to the currently registered doc string
  32.     generator and returns the result.
  33.     '''
  34.     return _generate_doc_string_func(info)
  35.  
  36. _type_tag_to_py_type = {
  37.     TypeTag.BOOLEAN: bool,
  38.     TypeTag.INT8: int,
  39.     TypeTag.UINT8: int,
  40.     TypeTag.INT16: int,
  41.     TypeTag.UINT16: int,
  42.     TypeTag.INT32: int,
  43.     TypeTag.UINT32: int,
  44.     TypeTag.INT64: int,
  45.     TypeTag.UINT64: int,
  46.     TypeTag.FLOAT: float,
  47.     TypeTag.DOUBLE: float,
  48.     TypeTag.GLIST: list,
  49.     TypeTag.GSLIST: list,
  50.     TypeTag.ARRAY: list,
  51.     TypeTag.GHASH: dict,
  52.     TypeTag.UTF8: str,
  53.     TypeTag.FILENAME: str,
  54.     TypeTag.UNICHAR: str,
  55.     TypeTag.INTERFACE: None,
  56.     TypeTag.GTYPE: None,
  57.     TypeTag.ERROR: None,
  58.     TypeTag.VOID: None }
  59.  
  60. def _get_pytype_hint(gi_type):
  61.     type_tag = gi_type.get_tag()
  62.     py_type = _type_tag_to_py_type.get(type_tag, None)
  63.     if py_type and hasattr(py_type, '__name__'):
  64.         return py_type.__name__
  65.     if None == TypeTag.INTERFACE:
  66.         iface = gi_type.get_interface()
  67.         info_name = iface.get_name()
  68.         if not info_name:
  69.             return gi_type.get_tag_as_string()
  70.         return None % (iface.get_namespace(), info_name)
  71.     return None.get_tag_as_string()
  72.  
  73.  
  74. def _generate_callable_info_doc(info):
  75.     in_args_strs = []
  76.     if isinstance(info, VFuncInfo):
  77.         in_args_strs = [
  78.             'self']
  79.     elif isinstance(info, FunctionInfo) and info.is_method():
  80.         in_args_strs = [
  81.             'self']
  82.     
  83.     args = info.get_arguments()
  84.     hint_blacklist = ('void',)
  85.     ignore_indices = set()
  86.     user_data_indices = set()
  87.     for arg in args:
  88.         ignore_indices.add(arg.get_destroy())
  89.         ignore_indices.add(arg.get_type().get_array_length())
  90.         user_data_indices.add(arg.get_closure())
  91.     
  92.     for i, arg in enumerate(args):
  93.         if arg.get_direction() == Direction.OUT:
  94.             continue
  95.         if i in ignore_indices:
  96.             continue
  97.         argstr = arg.get_name()
  98.         hint = _get_pytype_hint(arg.get_type())
  99.         if hint not in hint_blacklist:
  100.             argstr += ':' + hint
  101.         if arg.may_be_null() or i in user_data_indices:
  102.             argstr += '=None'
  103.         elif arg.is_optional():
  104.             argstr += '=<optional>'
  105.         in_args_strs.append(argstr)
  106.     
  107.     in_args_str = ', '.join(in_args_strs)
  108.     out_args_strs = []
  109.     return_hint = _get_pytype_hint(info.get_return_type())
  110.     if not info.skip_return() and return_hint and return_hint not in hint_blacklist:
  111.         argstr = return_hint
  112.         if info.may_return_null():
  113.             argstr += ' or None'
  114.         out_args_strs.append(argstr)
  115.     for i, arg in enumerate(args):
  116.         if arg.get_direction() == Direction.IN:
  117.             continue
  118.         if i in ignore_indices:
  119.             continue
  120.         argstr = arg.get_name()
  121.         hint = _get_pytype_hint(arg.get_type())
  122.         if hint not in hint_blacklist:
  123.             argstr += ':' + hint
  124.         out_args_strs.append(argstr)
  125.     
  126.     if out_args_strs:
  127.         return '%s(%s) -> %s' % (info.__name__, in_args_str, ', '.join(out_args_strs))
  128.     return None % (info.__name__, in_args_str)
  129.  
  130.  
  131. def _generate_class_info_doc(info):
  132.     header = '\n:Constructors:\n\n::\n\n'
  133.     doc = ''
  134.     if isinstance(info, StructInfo) or info.get_size() > 0:
  135.         doc += '    ' + info.get_name() + '()\n'
  136.     
  137.     doc += '    ' + info.get_name() + '(**properties)\n'
  138.     for method_info in info.get_methods():
  139.         if method_info.is_constructor():
  140.             doc += '    ' + _generate_callable_info_doc(method_info) + '\n'
  141.             continue
  142.     if doc:
  143.         return header + doc
  144.     return None
  145.  
  146.  
  147. def _generate_doc_dispatch(info):
  148.     if isinstance(info, (ObjectInfo, StructInfo)):
  149.         return _generate_class_info_doc(info)
  150.     if None(info, CallableInfo):
  151.         return _generate_callable_info_doc(info)
  152.  
  153. set_doc_string_generator(_generate_doc_dispatch)
  154.